package com.example.springmvcinterceptor.config;

import com.example.springmvcinterceptor.sertletComponent.MyFilter;
import com.example.springmvcinterceptor.sertletComponent.MyListener;
import com.example.springmvcinterceptor.sertletComponent.MyServle;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import java.util.Arrays;


@Configuration
public class ServletConfig {
    /**
     * servlet组建的注册
     */
    @Bean//把当前方法存到spring容器中/Servlet小服务程序
    public ServletRegistrationBean getServlet(MyServle myServle) {
        ServletRegistrationBean<MyServle> myServletServletRegistrationBean = new ServletRegistrationBean<>(myServle, "/myServlet");
        return myServletServletRegistrationBean;
    }



    @Bean
    //注册filter/过滤器
    public FilterRegistrationBean getFilter(MyFilter myFilter) {
        FilterRegistrationBean<MyFilter> myFilterFilterRegistrationBean = new FilterRegistrationBean<>(myFilter);
        myFilterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/toLoginPage"));
        return myFilterFilterRegistrationBean;


    }

    //Listener 监听器
    @Bean
    public ServletListenerRegistrationBean getListener(MyListener myListener){
        ServletListenerRegistrationBean<MyListener> myListenerServletListenerRegistrationBean=new ServletListenerRegistrationBean<>(myListener);
        return myListenerServletListenerRegistrationBean;
    }
}
